<?php

class Claycode_Router
{
	private $o_appController; 
	private $s_method;
	private static $o_instance;
	private $arr_constants;
	private $arr_applications;
	private $s_appRequestVarName;
	private $s_subAppRequestVarName;
	public $s_currAppIdent;
	
	public function __construct()
	{
		//include the applications declaration file
		include C_FW_customPackage . 'configs/applications.php';
		$this->arr_applications = $arr_applications;
		$this->s_appRequestVarName = $s_appRequestVarName;
		$this->s_subAppRequestVarName = $s_subAppRequestVarName;
		
		$this->arr_constants = array(
			'def_method'=> 'start',
			'controllers_dir'=> 'controllers',
			'controller_suffix'=> '',
			'error_controller'=> 'Error404Controller',
			'navi_request_delim'=> '/'
		);
		
		if(isset($_GET[$s_appRequestVarName])) {
			$this->manageAppRequests($_GET);
		} else if(isset($_POST['request'])) {
			$this->routeAjax($_POST['request']);
		} else {
			$this->getDefaultApp();
		}
		/*
		//for ajax requests
		if(isset($_POST['request'])){
			$this->routeAjax($_POST['request']);
		}
		*/
	}
	
	private function manageAppRequests($s_request)
	{
		if(!empty($s_request)){
			//get the necessary values
			//main app request
			if(isset($s_request[$this->s_appRequestVarName])){
				$this->o_appController = fw_util_getAppController($s_request[$this->s_appRequestVarName]);
			} else {
				$this->getDefaultApp();
			}
			//sub application request
			//by convention this is the method in the parent controller
			//if the sub applciation is controlled by a class. instantiate it via the parent controller in one of the methods
			if(isset($s_request[$this->s_subAppRequestVarName])){
				$arr_appArrConfig = fw_util_findArrValue($this->arr_applications , $this->o_appController);
				$arr_subAppArrConfig = fw_util_findArrValue($arr_appArrConfig['sub_applications'], $s_request[$this->s_subAppRequestVarName]);
				$this->s_method = $arr_subAppArrConfig['controller_class'];
			} else {
				$this->s_method = $this->arr_constants['def_method'];
			}
		}
		/*
		$arr_appRequestParts = explode($this->arr_constants['navi_request_delim'],trim($s_appRequest,$this->arr_constants['navi_request_delim']));
		//for app requests
		if(!empty($arr_appRequestParts[0])) {
			//make upper case the first letter
			//$this->o_appController = ucfirst($arr_appRequestParts[0]);
			$this->o_appController = fw_util_getAppController($arr_appRequestParts[0]);
		
		} else {
			$this->getDefaultApp();
		}
		
		//for the method to call
		if(!empty($arr_appRequestParts[1])) {
			$this->s_method = $arr_appRequestParts[1];
		} else {
			$this->s_method = $this->arr_constants['def_method'];
		}
		*/
		
	}
	
	private function getDefaultApp()
	{
		//include the default configuration file that is in the custom package
		include C_FW_customPackage.'configs/defaults.php';
		$this->o_appController = $s_defaultController;
		$this->s_method = $this->arr_constants['def_method'];
	}
	
	public static function getInstance()
	{
		if(!isset(self::$o_instance) ||  self::$o_instance == null) {
			self::$o_instance = new Claycode_Router();
		}
		return self::$o_instance;
	}
	
	public function route()
	{
		$file = C_FW_customPackage
			  . $this->arr_constants['controllers_dir'] 
			  . '/'  
			  . $this->o_appController 
			  . $this->arr_constants['controller_suffix'] 
			  . '.php';
		
		
		if(is_readable($file)) {
			include $file;
			
			$s_className = $this->o_appController . $this->arr_constants['controller_suffix'];
		} else {
			/*
			//launch the default app on a no controller found occurence
			$this->getDefaultApp();
			include C_FW_customPackage
				  . $this->arr_constants['controllers_dir'] 
				  . '/'  
				  . $this->o_appController 
				  . $this->arr_constants['controller_suffix'] 
				  . '.php';
				
			$s_className = $this->o_appController . $this->arr_constants['controller_suffix'];
			*/
			
			//if you want the default to be the error controller uncomment this
			include C_FW_customPackage .$this->arr_constants['controllers_dir'] . '/' .$this->arr_constants['error_controller']  .'.php';
			$s_className = $this->arr_constants['error_controller'];
			
		}
		$this->s_currAppIdent = fw_util_getAppIdent($this->o_appController);
		$o_controller = new $s_className();
		
		if (is_callable(array($o_controller, $this->s_method))) {
			$s_action = $this->s_method;
		} else {
			$s_action = $this->arr_constants['def_method'];
		}
		
		$o_controller->$s_action();
		
	}
	
	private function routeAjax($arr_request)
	{
		//die(fw_util_outputJson(array( 'serviceName'=> $arr_request['serviceName'])));
		if(isset($arr_request['serviceName'])) {
			switch ($arr_request['serviceName'])
			{
				case 'GetHeaderTitle':
					//die(fw_util_outputJson(array( 'args'=> $arr_request['args'])));
					echo fw_util_outputJson(array( 'args'=> $arr_request['args']));
					break;
			}
		}
		
	}
	
}